<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  
  <link rel="shortcut icon" href="../img/favicon.ico">
  <title>Rewinding - Forge Networking Remastered</title>
  <link href='https://fonts.googleapis.com/css?family=Lato:400,700|Roboto+Slab:400,700|Inconsolata:400,700' rel='stylesheet' type='text/css'>

  <link rel="stylesheet" href="../css/theme.css" type="text/css" />
  <link rel="stylesheet" href="../css/theme_extra.css" type="text/css" />
  <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/styles/github.min.css">
  
  <script>
    // Current page data
    var mkdocs_page_name = "Rewinding";
    var mkdocs_page_input_path = "rewinding.md";
    var mkdocs_page_url = null;
  </script>
  
  <script src="../js/jquery-2.1.1.min.js" defer></script>
  <script src="../js/modernizr-2.8.3.min.js" defer></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/highlight.js/9.12.0/highlight.min.js"></script>
  <script>hljs.initHighlightingOnLoad();</script> 
  
</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side stickynav">
      <div class="wy-side-nav-search">
        <a href=".." class="icon icon-home"> Forge Networking Remastered</a>
        <div role="search">
  <form id ="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" title="Type search term here" />
  </form>
</div>
      </div>

      <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
	<ul class="current">
	  
          
            <li class="toctree-l1">
		
    <span class="caption-text">Home</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="..">User Manual (Forge Networking Remastered)</a>
                </li>
                <li class="">
                    
    <a class="" href="../authoritative-design/">Authoritative Design</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Getting Started</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../GettingStarted/getting-started/">Getting Started</a>
                </li>
                <li class="">
                    
    <a class="" href="../GettingStarted/basic-moving-cube-example/">Basic Moving Cube Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../GettingStarted/basic-rpc-example/">Basic RPC Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../GettingStarted/basic-instantiation-example/">Basic Instantiation Example</a>
                </li>
                <li class="">
                    
    <a class="" href="../GettingStarted/jump-start-guide/">Jump Start Guide</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Network Contract Wizard (NCW)</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../NetworkContractWizard/network-contract-wizard-ncw/">Network Contract Wizard (NCW)</a>
                </li>
                <li class="">
                    
    <a class="" href="../NetworkContractWizard/extending-generated-classes/">Extending Generated Classes</a>
                </li>
                <li class="">
                    
    <a class="" href="../NetworkContractWizard/name-collision-issues/">Name Collision Issues</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Network Object</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../NetworkObject/changing-ownership/">Changing Ownership</a>
                </li>
                <li class="">
                    
    <a class="" href="../NetworkObject/destroying-the-network-object/">Destroying the Network Object</a>
                </li>
                <li class="">
                    
    <a class="" href="../NetworkObject/fields/">Fields</a>
                </li>
                <li class="">
                    
    <span class="caption-text">Remote Procedure Calls (RPCs)</span>
    <ul class="subnav">
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/remote-procedure-calls/">Remote Procedure Calls</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/buffered-rpcs/">Buffered RPCs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/sending-rpc-to-a-single-player/">Sending RPC to a Single Player</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/rpcargs-and-rpcinfo-structs/">RpcArgs and RpcInfo Structs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/rpc-validation-by-server/">RPC Validation by Server</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/replacing-previous-buffered-rpcs/">Replacing Previous Buffered RPCs</a>
                </li>
                <li class="toctree-l3">
                    
    <a class="" href="../NetworkObject/RemoteProcedureCalls/clearing-buffered-rpcs/">Clearing Buffered RPCs</a>
                </li>
    </ul>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Unity Integration</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../UnityIntegration/gameobject-execution-order/">GameObject Execution Order</a>
                </li>
                <li class="">
                    
    <a class="" href="../UnityIntegration/network-instantiation/">Network Instantiation</a>
                </li>
                <li class="">
                    
    <a class="" href="../UnityIntegration/threading-in-unity/">Threading in Unity</a>
                </li>
                <li class="">
                    
    <a class="" href="../UnityIntegration/main-threading-rpcs/">Main Threading RPCs</a>
                </li>
                <li class="">
                    
    <a class="" href="../UnityIntegration/running-unity-specific-code-on-the-main-thread/">Running Unity specific code on the main thread</a>
                </li>
                <li class="">
                    
    <a class="" href="../UnityIntegration/network-start/">Unity Integration Network Start</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Basic Network Samples</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../BasicNetworkSamples/server-hosting-code/">Server Hosting Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../BasicNetworkSamples/client-connecting-code/">Client Connecting Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../BasicNetworkSamples/server-disconnect-client-code/">Server Disconnect Client Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../BasicNetworkSamples/ban-client-code/">Ban Client Code</a>
                </li>
                <li class="">
                    
    <a class="" href="../BasicNetworkSamples/send-binary-frame/">Send Binary Frame</a>
                </li>
                <li class="">
                    
    <a class="" href="../BasicNetworkSamples/send-file/">Send File</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Scene Navigation</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../SceneNavigation/loading-scenes/">Loading Scenes</a>
                </li>
                <li class="">
                    
    <a class="" href="../SceneNavigation/scene-events/">Scene Events</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">NetWorker</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../NetWorker/temporarily-blocking-connections/">Temporarily Blocking Connections</a>
                </li>
                <li class="">
                    
    <a class="" href="../NetWorker/thread-safe-player-iteration/">Thread Safe Player Iteration</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Master Server</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../MasterServer/quick-start/">Master Server Quick Start</a>
                </li>
                <li class="">
                    
    <a class="" href="../MasterServer/connecting-to-master-server/">Connecting to Master Server</a>
                </li>
                <li class="">
                    
    <a class="" href="../MasterServer/getting-host-list/">Getting Host List From Master Server</a>
                </li>
                <li class="">
                    
    <a class="" href="../MasterServer/windows-server-firewall-setup/">Windows Server Firewall Setup</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Web Server</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../WebServer/jumpstart/">Web Server Jumpstart</a>
                </li>
                <li class="">
                    
    <a class="" href="../WebServer/extending-with-mvc/">Web Server Extending With MVC</a>
                </li>
                <li class="">
                    
    <a class="" href="../WebServer/command-plugins/">Web Server Command Plugins</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <span class="caption-text">Troubleshooting</span>
    <ul class="subnav">
                <li class="">
                    
    <a class="" href="../Troubleshooting/troubleshooting/">Troubleshooting</a>
                </li>
                <li class="">
                    
    <a class="" href="../Troubleshooting/throttling-network-internal/">Throttling Network Internal</a>
                </li>
    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../connection-cycle-events/">Events</a>
	    </li>
          
            <li class="toctree-l1 current">
		
    <a class="current" href="./">Rewinding</a>
    <ul class="subnav">
            
    <li class="toctree-l2"><a href="#rewinding">Rewinding</a></li>
    
        <ul>
        
            <li><a class="toctree-l3" href="#deeper-dive">Deeper Dive</a></li>
        
            <li><a class="toctree-l3" href="#other-get-methods">Other Get Methods</a></li>
        
        </ul>
    

    </ul>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../network-logging/">Debugging</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../working-with-multiple-sockets/">Working With Multiple Sockets</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../nat-hole-punching/">NAT Hole Punching</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../lan-discovery/">LAN Discovery</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../offline-mode/">Offline Mode</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../lobby/">Lobby System</a>
	    </li>
          
            <li class="toctree-l1">
		
    <a class="" href="../adding-and-updating-docs/">Contributing</a>
	    </li>
          
        </ul>
      </div>
      &nbsp;
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="..">Forge Networking Remastered</a>
      </nav>

      
      <div class="wy-nav-content">
        <div class="rst-content">
          <div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="..">Docs</a> &raquo;</li>
    
      
    
    <li>Rewinding</li>
    <li class="wy-breadcrumbs-aside">
      
    </li>
  </ul>
  <hr/>
</div>
          <div role="main">
            <div class="section">
              
                <h1 id="rewinding">Rewinding</h1>
<p>Rewinding is a method in which you can go back in "time" and look at what the value of a variable was at/around a specific timestep. The <code>Rewind</code> data structure is a pretty simple structure that allows you to do rewinding however you would like. Since you are able to get the Timestep at any point in your code through the use of <code>NetworkManager.Instance.NetWorker.Time.Timestep</code>, you can easily store anything into the structure that you need. Below is an example showing you how to get started with rewinding in a practical way.</p>
<ol>
<li>Create a C# file in your project</li>
<li>Name the file <code>RewindTest.cs</code></li>
<li>Open the file and paste the code following these steps</li>
<li>Create a new scene</li>
<li>Place a cube in the scene</li>
<li>Attach the <code>RewindTeste.cs</code> script to the cube</li>
<li>Attach a <code>Rigidbody</code> component to the cube</li>
<li>Save the scene as <strong>RewindTest</strong></li>
<li>Open the Multiplayer Menu sample scene</li>
<li>Open the build settings</li>
<li>Click the <strong>Player Settings...</strong> button</li>
<li>Make sure <strong>Run in Background</strong> is turned on</li>
<li>Add the Multiplayer Menu Scene as scene (index 0) to the build settings</li>
<li>Add the <strong>RewindTest</strong> scene as the second scene (index 1) to the build settings</li>
<li>Press play</li>
<li>Host a game</li>
<li>Wait a second or two</li>
<li>Press the spacebar</li>
<li>Check the <strong>GET</strong> message and what time it was at</li>
<li>Check the <strong>SET</strong> message with the closest time to that seen in the get message and you will notice they are a match</li>
</ol>
<pre><code class="csharp">using BeardedManStudios.Forge.Networking;
using BeardedManStudios.Forge.Networking.Unity;
using System.Collections;
using UnityEngine;

public class RewindTest : MonoBehaviour
{
    private Rewind&lt;Vector3&gt; rewind;

    private void Start()
    {
        rewind = new Rewind&lt;Vector3&gt;(5000);
        StartCoroutine(Store());
    }

    private void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            var timestep = NetworkManager.Instance.Networker.Time.Timestep - 1000;
            Debug.Log(&quot;GET: &quot; + timestep + &quot; | &quot; + rewind.Get(timestep));
            UnityEditor.EditorApplication.isPaused = true;
        }
    }

    private IEnumerator Store()
    {
        for (;;)
        {
            var timestep = NetworkManager.Instance.Networker.Time.Timestep;
            rewind.Register(transform.position, timestep);
            Debug.Log(&quot;SET: &quot; + timestep + &quot; | &quot; + transform.position);
            yield return new WaitForSeconds(0.5f);
        }
    }
}
</code></pre>

<h2 id="deeper-dive">Deeper Dive</h2>
<p>You will notice in the above example we do <code>rewind = new Rewind&lt;Vector3&gt;(5000);</code>. The <code>5000</code> in this case is the number of milliseconds to track data. Anything older than 5 seconds from now will be removed for memory constraint purposes. You can put whatever time frame you want here, 5000 may be a bit much (because 5000ms is 5 seconds which is a very long time). The <code>&lt;Vector3&gt;</code> is the type of data that we are storing into this rewinding structure.</p>
<p>Next you will notice the <code>rewind.Register(transform.position, timestep);</code> call. This will save the value of <code>transform.position</code> to the rewind history at the provided <code>ulong</code> <strong>timestep</strong>. This adds to the pool of stored data for this specific variable for you to pull from. <strong>Note</strong> It is not good practice to <code>Register</code> different <code>Vector3</code> addresses, so if we are using <code>rewind</code> for <code>transform.position</code> we should only pass <code>transform.position</code> into this variable's <code>Register</code> method.</p>
<p>Lastly we come to the most important action we can do <code>rewind.Get(timestep)</code>. There are many different overloads for getting the values near and around a timestep however the simplest method to call is <code>Get</code>. So let's say you <code>Register</code> a value <strong>A</strong> at timestep <strong>3</strong> and a value <strong>B</strong> at timestep <strong>20</strong>. If you were to call <code>Get(10)</code> then you will get value <strong>A</strong> returned, however if you called <code>Get(19)</code> you would get the value <strong>B</strong>. This is because the value closest to the sent timestamp would be returned.</p>
<h2 id="other-get-methods">Other Get Methods</h2>
<p><code>List&lt;T&gt; Get(ulong timestep, out T lower, out T upper)</code> - If we were following the above example and we passed <code>Get(10, out lower, out upper)</code> into this method, then <code>lower</code> would equal <strong>3</strong>, <code>upper</code> would equal <strong>20</strong> and the returned value will be <strong>3</strong>. This method is useful for if you want to calculate a median or where the value might possibly be between the two values.</p>
<p><code>List&lt;T&gt; GetRange(ulong timestep, int count)</code> - This method will act exactly like the first <code>Get</code> method we learned about in the <strong>Deeper Dive</strong> section above; however, it also will return <strong>n</strong> values before the given timestep. So if we passed in <strong>3</strong> for the <code>count</code> then we would get a list of values back where the first element is the target value for that timestep and every element after that is the value stored at the last timestep registered.</p>
<p><code>T GetRange(ulong timestepMin, ulong timestepMax)</code> - This method allows you to get all values between two timesteps (including the min and the max).</p>
              
            </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="../network-logging/" class="btn btn-neutral float-right" title="Debugging">Next <span class="icon icon-circle-arrow-right"></span></a>
      
      
        <a href="../connection-cycle-events/" class="btn btn-neutral" title="Events"><span class="icon icon-circle-arrow-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <!-- Copyright etc -->
    
  </div>

  Built with <a href="http://www.mkdocs.org">MkDocs</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
      
        </div>
      </div>

    </section>

  </div>

  <div class="rst-versions" role="note" style="cursor: pointer">
    <span class="rst-current-version" data-toggle="rst-current-version">
      
      
        <span><a href="../connection-cycle-events/" style="color: #fcfcfc;">&laquo; Previous</a></span>
      
      
        <span style="margin-left: 15px"><a href="../network-logging/" style="color: #fcfcfc">Next &raquo;</a></span>
      
    </span>
</div>
    <script>var base_url = '..';</script>
    <script src="../js/theme.js" defer></script>
      <script src="../search/main.js" defer></script>

</body>
</html>
